//
//  LZBannerView.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "LZBannerView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

@interface LZBannerView () <SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSArray<BannerModel *> *banners;

@end

@implementation LZBannerView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDefaultConfig];
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self setupDefaultConfig];
        [self setupUI];
    }
    return self;
}

+ (instancetype)bannerViewWithFrame:(CGRect)frame delegate:(id<LZBannerViewDelegate>)delegate {
    LZBannerView *bannerView = [[self alloc] initWithFrame:frame];
    bannerView.delegate = delegate;
    return bannerView;
}

#pragma mark - 配置
- (void)setupDefaultConfig {
    _autoScroll = YES;
    _autoScrollInterval = 3.0;
    _showPageControl = YES;
    _infiniteLoop = YES;
    _placeholderImage = [UIImage imageNamed:@"banner_placeholder"];
    _currentPageDotColor = [UIColor whiteColor];
    _pageDotColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    _imageContentMode = UIViewContentModeScaleAspectFill;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];

    // 创建轮播图
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.bounds delegate:self placeholderImage:_placeholderImage];
    _cycleScrollView.autoScroll = _autoScroll;
    _cycleScrollView.autoScrollTimeInterval = _autoScrollInterval;
    _cycleScrollView.showPageControl = _showPageControl;
    _cycleScrollView.infiniteLoop = _infiniteLoop;
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _cycleScrollView.currentPageDotColor = _currentPageDotColor;
    _cycleScrollView.pageDotColor = _pageDotColor;
    _cycleScrollView.bannerImageViewContentMode = _imageContentMode;
    _cycleScrollView.pageControlBottomOffset = 10;
    _cycleScrollView.pageControlRightOffset = 15;

    [self addSubview:_cycleScrollView];
}

#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    _cycleScrollView.frame = self.bounds;
}

#pragma mark - 公共方法
- (void)setBanners:(NSArray<BannerModel *> *)banners {
    _banners = banners;
    [self updateCycleScrollView];
}

- (void)setAutoScroll:(BOOL)autoScroll {
    _autoScroll = autoScroll;
    _cycleScrollView.autoScroll = autoScroll;
}

- (void)setAutoScrollInterval:(NSTimeInterval)autoScrollInterval {
    _autoScrollInterval = autoScrollInterval;
    _cycleScrollView.autoScrollTimeInterval = autoScrollInterval;
}

- (void)setShowPageControl:(BOOL)showPageControl {
    _showPageControl = showPageControl;
    _cycleScrollView.showPageControl = showPageControl;
}

- (void)setInfiniteLoop:(BOOL)infiniteLoop {
    _infiniteLoop = infiniteLoop;
    _cycleScrollView.infiniteLoop = infiniteLoop;
}

- (void)setPlaceholderImage:(UIImage *)placeholderImage {
    _placeholderImage = placeholderImage;
    _cycleScrollView.placeholderImage = placeholderImage;
}

- (void)setCurrentPageDotColor:(UIColor *)currentPageDotColor {
    _currentPageDotColor = currentPageDotColor;
    _cycleScrollView.currentPageDotColor = currentPageDotColor;
}

- (void)setPageDotColor:(UIColor *)pageDotColor {
    _pageDotColor = pageDotColor;
    _cycleScrollView.pageDotColor = pageDotColor;
}

- (void)setImageContentMode:(UIViewContentMode)imageContentMode {
    _imageContentMode = imageContentMode;
    _cycleScrollView.bannerImageViewContentMode = imageContentMode;
}

- (void)startAutoScroll {
    _cycleScrollView.autoScroll = YES;
}

- (void)stopAutoScroll {
    _cycleScrollView.autoScroll = NO;
}

- (void)reloadData {
    [self updateCycleScrollView];
}

#pragma mark - 私有方法
- (void)updateCycleScrollView {
    if (self.banners.count == 0) {
        _cycleScrollView.localizationImageNamesGroup = nil;
        _cycleScrollView.imageURLStringsGroup = nil;
        _cycleScrollView.titlesGroup = nil;
        return;
    }

    NSMutableArray *imageURLs = [NSMutableArray array];
    NSMutableArray *titles = [NSMutableArray array];

    for (BannerModel *banner in self.banners) {
        if (banner.img) {
            [imageURLs addObject:banner.img];
            [titles addObject:banner.title ?: @""];
        }
    }

    _cycleScrollView.imageURLStringsGroup = imageURLs;
    _cycleScrollView.titlesGroup = titles;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (index < self.banners.count) {
        BannerModel *banner = self.banners[index];
        if ([self.delegate respondsToSelector:@selector(bannerView:didSelectBanner:atIndex:)]) {
            [self.delegate bannerView:self didSelectBanner:banner atIndex:index];
        }
    }
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    if ([self.delegate respondsToSelector:@selector(bannerView:didScrollToIndex:)]) {
        [self.delegate bannerView:self didScrollToIndex:index];
    }
}

@end
